package com.itheima.wms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.wms.common.enums.IncreaseDecreaseStatus;
import com.itheima.wms.common.enums.StockRecordType;
import com.itheima.wms.common.service.CrudServiceImpl;
import com.itheima.wms.dao.IncreaseDecreaseMapper;
import com.itheima.wms.dao.StockRecordMapper;
import com.itheima.wms.dto.IncreaseDecreaseDTO;
import com.itheima.wms.entity.IncreaseDecreaseEntity;
import com.itheima.wms.entity.StockEntity;
import com.itheima.wms.entity.StockRecordEntity;
import com.itheima.wms.service.IncreaseDecreaseService;
import com.itheima.wms.service.StockService;
import com.itheima.wms.vo.IncreaseDecreaseDetailVO;
import com.itheima.wms.vo.IncreaseDecreaseSumVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 业务实现类
 * 损益单
 * </p>
 */
@Slf4j
@Service
public class IncreaseDecreaseServiceImpl extends CrudServiceImpl<IncreaseDecreaseMapper, IncreaseDecreaseEntity> implements IncreaseDecreaseService {
    @Autowired
    private StockService stockService;
    @Autowired
    private StockRecordMapper stockRecordMapper;

    @Override
    public IPage<IncreaseDecreaseDetailVO> pageDetail(Page<IncreaseDecreaseDetailVO> page, Map data) {
        IPage<IncreaseDecreaseDetailVO> iPage = this.baseMapper.selectIncreaseDecreaseDetail(page, data);
        return iPage;
    }

    @Override
    public List<IncreaseDecreaseDetailVO> listDetail(Map data) {
        return this.baseMapper.selectIncreaseDecreaseDetail(data);
    }

    @Override
    @Transactional
    public void change(IncreaseDecreaseDTO data) {
        IncreaseDecreaseEntity increaseDecreaseEntity = this.baseMapper.selectById(data.getId());
        increaseDecreaseEntity.setStatus(IncreaseDecreaseStatus.CHANGE.getCode());
        this.updateById(increaseDecreaseEntity);

        StockEntity stockEntity = stockService.getById(increaseDecreaseEntity.getStockId());
        Integer original = stockEntity.getTotal();
        Integer originalFree = stockEntity.getFree();

        stockEntity.setTotal(stockEntity.getTotal() + increaseDecreaseEntity.getIdNum());
        stockEntity.setFree(stockEntity.getTotal() - stockEntity.getFrozen());
        stockService.saveOrUpdate(stockEntity);

        StockRecordEntity stockRecordEntity = new StockRecordEntity();
        stockRecordEntity.setSourceId(increaseDecreaseEntity.getId());
        stockRecordEntity.setLocationId(stockEntity.getLocationId());
        stockRecordEntity.setType(StockRecordType.SY.getCode());// 类型损益
        stockRecordEntity.setOriginal(original);
        stockRecordEntity.setOriginalFree(originalFree);
        stockRecordEntity.setWay(increaseDecreaseEntity.getIdNum() > 0 ? "+" : "-");
        stockRecordEntity.setAlteration(increaseDecreaseEntity.getIdNum());
        stockRecordEntity.setResult(stockRecordEntity.getOriginal() + stockRecordEntity.getAlteration());
        stockRecordEntity.setResultFree(stockRecordEntity.getOriginalFree() + stockRecordEntity.getAlteration());
        stockRecordMapper.insert(stockRecordEntity);
    }

    @Override
    public void cancel(IncreaseDecreaseDTO data) {
        IncreaseDecreaseEntity increaseDecreaseEntity = this.baseMapper.selectById(data.getId());
        increaseDecreaseEntity.setStatus(IncreaseDecreaseStatus.CANCEL.getCode());
        this.updateById(increaseDecreaseEntity);
    }

    @Override
    public IncreaseDecreaseSumVO sum(Map data) {
        return this.baseMapper.sumIncreaseDecrease(data);
    }
}

